package com.android.webttest.Adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.webttest.R;
import com.android.webttest.model.GoodComments;
import com.android.webttest.model.GoodCommentsPro;

import java.util.List;

public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.CommentViewHolder> {

    private final List<GoodCommentsPro> commentList;

    public CommentAdapter(List<GoodCommentsPro> commentList) {
        this.commentList = commentList;
    }

    @NonNull
    @Override
    public CommentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_comment, parent, false);
        return new CommentViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CommentViewHolder holder, int position) {
        GoodCommentsPro comment = commentList.get(position);

        TextView textView = holder.itemView.findViewById(R.id.comment_text);
        textView.setText(comment.getCommentsText());

        // 设置一级评论样式
        if (comment.getParentCommentId() == null) {
            textView.setPadding(16, 0, 0, 0); // 一级评论无缩进
        } else {
            textView.setPadding(48, 0, 0, 0); // 二级评论增加缩进
        }

        // 如果有子评论，绑定子评论
        RecyclerView subRecyclerView = holder.itemView.findViewById(R.id.sub_comment_recycler_view);
        if (comment.getSubGoodComments() != null && !comment.getSubGoodComments().isEmpty()) {
            subRecyclerView.setLayoutManager(new LinearLayoutManager(holder.itemView.getContext()));
            subRecyclerView.setAdapter(new SubCommentAdapter(comment.getSubGoodComments()));
        } else {
            subRecyclerView.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return commentList.size();
    }

    static class CommentViewHolder extends RecyclerView.ViewHolder {
        public CommentViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
}